﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace BDSim
{
    /// <summary>
    /// 电离层TEC模型
    /// </summary>
    public class IonTECSimModel : SpaceEnvModel
    {
        #region 字段和属性
        /// <summary>
        /// 右键命令集合
        /// </summary>
        private List<MenuCommand> MenuCommandList = new List<MenuCommand>();

        /// <summary>
        /// 电离层仿真模型类型
        /// </summary>
        private IonSimModelType _SimModelType = IonSimModelType.球谐模型;
        [XmlAttribute, CategoryAttribute("模型设置"), DescriptionAttribute("电离层计算模型类型选择")]
        public IonSimModelType SimModelType
        {
            get { return _SimModelType; }
            set { _SimModelType = value; }
        }

        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public IonTECSimModel()
        {
            MenuCommand IonTEC = new MenuCommand();
            IonTEC.Header = "VTEC";
            //二维显示
            MenuCommand TwoDimensionShow = new MenuCommand();
            TwoDimensionShow.Header = "二维显示";
            TwoDimensionShow.DefineExcuteCommand(TwoDimensionShowFun);
            //导出文件
            MenuCommand ExportFile = new MenuCommand();
            ExportFile.Header = "导出文件";
            ExportFile.DefineExcuteCommand(ExportFileFun);

            //右键命令组织形式
            IonTEC.SubMenuCommandList.Add(TwoDimensionShow);
            IonTEC.SubMenuCommandList.Add(ExportFile);
            MenuCommandList.Add(IonTEC);
            Menu.ItemsSource = MenuCommandList;
        }

        #endregion

        #region 方法
        /// <summary>
        /// VTEC二维显示函数
        /// </summary>
        /// <param name="param"></param>
        private void TwoDimensionShowFun(object param)
        {

        }

        /// <summary>
        /// VTEC导出文件函数
        /// </summary>
        /// <param name="param"></param>
        private void ExportFileFun(object param)
        {
            string filename = currentDirectory + "\\DataRecords\\环境段\\电离层VTEC数据_"+
                DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
            ExportSpaceEnvFile vtecFile = new ExportSpaceEnvFile(2, filename);
            vtecFile.ShowDialog();
            if (vtecFile.SelResult == true)
            {
                Thread ThreadExportFile = new Thread(new ParameterizedThreadStart(ThreadExportFileFun));
                ThreadExportFile.Start(vtecFile.FileName);
            }
        }

        private void ThreadExportFileFun(object obj)
        {
            UTCTime start = BDSimObject.GlobalStartTime;
            UTCTime end = BDSimObject.GlobalEndTime;

            BLH_PosVel pos = new BLH_PosVel();
            double vtec = 0;

            IonVTECWriter VTECWriter = new IonVTECWriter();
            VTECWriter.InitIonVTECDataFile((string)obj);

            IonTECInit();
            for (UTCTime time = start; time.CompareTo(end) <= 0; time.AddSecond(IntervalTime))
            {
                for (double B = -87.5; B <= 87.5; B = B + IntervalBLH.B) //纬度
                {
                    for (double L = -180.0; L <= 180.0; L = L + IntervalBLH.L) //经度
                    {
                        pos.B = B;
                        pos.L = L;
                        pos.H = 0;
                        vtec = GetIonTEC(time, pos);
                        VTECWriter.WriteFile(time, B, L, vtec);
                    }
                }
            }
            MessageBox.Show("导出电离层VTEC完成", "提示");
        }

        /// <summary>
        /// 电离层TEC动态库初始化函数
        /// </summary>
        /// <returns></returns>
        private bool IonTECInit()
        {
            if (Enabled)
            {
                bool retv = false;
                UTCTime time = BDSimObject.GlobalStartTime;

                int ionType = 0;
                switch (SimModelType)
                {
                    case IonSimModelType.球谐模型:
                        ionType = 1;
                        break;
                    case IonSimModelType.全球格网插值模型:
                        ionType = 2;
                        break;
                    default:
                        break;
                }

                int ret = BDSim.IonTEC.CalIon_init(time, ionType);
                if (ret == 1)
                {
                    retv = true;
                }

                return retv;
            }

            return true;
        }

        /// <summary>
        /// 获取电离层电子总量
        /// </summary>
        /// <param name="time"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public double GetIonTEC(UTCTime time, BLH_PosVel pos)
        {
            double ret = 0.0;

            if (Enabled)
            {
                IonGridData ionGridData;
                ionGridData.time = time;
                ionGridData.Lat = pos.B;
                ionGridData.Lon = pos.L;

                int ionType = 0;
                switch (SimModelType)
                {
                    case IonSimModelType.球谐模型:
                        ionType = 1;
                        break;
                    case IonSimModelType.全球格网插值模型:
                        ionType = 2;
                        break;
                    default:
                        break;
                }

                BDSim.IonTEC.CalGirdIonTEC(out ionGridData, ionType);
                ret = ionGridData.VTEC;
            }

            return ret;
        }        

        /// <summary>
        /// 初始化函数
        /// </summary>
        public override bool Init()
        {
            return IonTECInit();
        }

        #endregion
    }
}
